package com.nblog.controller.front;
import java.util.Date;
import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.StrKit;
import com.nblog.annotation.ControllerBind;
import com.nblog.constant.ApiConstant;
import com.nblog.interceptor.front.WebsiteInterceptor;
import com.nblog.model.User;
import com.nblog.plugin.ioc.annotation.JFAutowired;
import com.nblog.resp.ApiResp;
import com.nblog.service.UserLoginRecordService;
import com.nblog.service.UserService;
import com.nblog.utils.CommonUtils;
import com.nblog.utils.IWebUtils;
import com.nblog.utils.encrypt.Md5Utils;

@ControllerBind(controllerKey="/auth")
@Before(WebsiteInterceptor.class)
public class AuthController extends BaseControlller {
	@JFAutowired
	UserService userService;
	@JFAutowired
	UserLoginRecordService userLoginRecordService;
	public void login(){
		String url=getPara("url", "");
		setAttr("url", url);
		render("login.html");
	}
	public void reg(){
		render("reg.html");
	}
	@Before(POST.class)
	public void ajaxReg(){
		String username=getPara("username");
		String password=getPara("password");
		ApiResp resp=new ApiResp();
		if(StrKit.isBlank(username)){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("请输入用户名");
			renderJson(resp);
			return;
		}
		if(StrKit.isBlank(password)){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("请输入密码");
			renderJson(resp);
			return;
		}
		if(userService.isexist(username)){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("该企业名称已被注册，请重新填写");
			renderJson(resp);
			return;
		}
		password=new Md5Utils().getMD5(password);
		User user=new User();
		user.setUsername(username);
		user.setEnterprisename(username);
		user.setPassword(password);
		user.setCreatetime(new Date());
		user.setUpdatetime(new Date());
		if(user.save()){
			resp.setCode(ApiConstant.CODE_SUCCESS);
			resp.setMsg("恭喜你，注册成功");
			renderJson(resp);
		}else{
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("不好意思，注册失败");
			renderJson(resp);
		}
	}
	@Before(POST.class)
	public void ajaxLogin(){
		String username=getPara("username");
		String password=getPara("password");
		Integer autoLogin=this.getParaToInt("autoLogin",0);
		ApiResp resp=new ApiResp();
		if(StrKit.isBlank(username)){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("请输入用户名");
			renderJson(resp);
			return;
		}
		if(StrKit.isBlank(password)){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("请输入密码");
			renderJson(resp);
			return;
		}
		User user=User.dao.findFirst("select * from user where username=?",username);
		if(user==null){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("不存在该用户");
			renderJson(resp);
			return;
		}
		if(userLoginRecordService.hasOverLoginErrTimes(user.getId())){
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("今天连续输入密码错误次数超过5次");
			renderJson(resp);
			return;
		}
		String ip=CommonUtils.getIP(getRequest());
		password=new Md5Utils().getMD5(password);
		if(!user.getPassword().equals(password)){
			userLoginRecordService.saveSysLoginRecord(user.getId(),0,ip);
			resp.setCode(ApiConstant.CODE_FAIL);
			resp.setMsg("用户密码输入有误");
			renderJson(resp);
			return;
		}
		IWebUtils.setCurrentLoginSysUser(this.getResponse(),this.getSession(),user,autoLogin);
		userLoginRecordService.saveSysLoginRecord(user.getId(),1,ip);
		resp.setCode(ApiConstant.CODE_SUCCESS);
		resp.setMsg("登录成功");
		renderJson(resp);
	}
	
	public void xieyi(){
		render("xieyi.html");
	}
}
